import 'package:flutter/material.dart';

double withPrecision4(double value) {
  return ((value * 10000).roundToDouble()) / 10000;
}

bool insideMainRect(Rect rectMain, Rect rectIn) {
  return rectIn.top >= rectMain.top &&
      rectIn.left >= rectMain.left &&
      rectIn.right <= rectMain.right &&
      rectIn.bottom <= rectMain.bottom;
}

Rect optPosition(Rect r1Prev, Rect r1, Rect r2) {
  if (r1.overlaps(r2)) {
    final overlap = r1.intersect(r2);

    if (overlap.height >= overlap.width) {
      return r1Prev;
    }
    if (overlap.width >= overlap.height) {
      return r1Prev;
    }
  }

  final res = Rect.fromLTWH(
    withPrecision4(r1.left),
    withPrecision4(r1.top),
    withPrecision4(r1.width),
    withPrecision4(r1.height),
  );

  return res;
}

double convertRadiusToSigma(double radius) {
  return radius * 0.57735 + 0.5;
}
